package com.matou.commons.handler;
import com.matou.commons.constants.Resp;
import com.matou.commons.exception.MtError;
import com.matou.commons.util.BaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author CM
 * @Date 2021/5/9 9:36
 * @Description CmAuthExceptionEntryPoint 处理资源服务器异常 主要处理 令牌错误 401
 * @Version 1.0
 */
@Slf4j
public class MaTouAuthExceptionEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException authException) throws IOException {
        log.error(authException.getMessage(),authException);
        BaseUtil.makeResponse(response, MediaType.APPLICATION_JSON_UTF8_VALUE, HttpServletResponse.SC_UNAUTHORIZED, Resp.failure(MtError.MT_001002));
    }
}
